package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.CombinedGuaranteeConfirm;
import com.unionpay.upyzt.resp.CombinedGuaranteeConfirmResp;

import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

public class CombinedGuaranteeConfirmExample {

    private final String guaranteePaymentId;

    public CombinedGuaranteeConfirmExample(String guaranteePaymentId) {
        this.guaranteePaymentId = guaranteePaymentId;
    }

    /**
     * 运行 Demos
     *
     * @param guaranteePaymentId 担保下单ID
     * @return 合并消费担保确认ID
     */
    public static String runDemos(String guaranteePaymentId) throws UpyztException {
        CombinedGuaranteeConfirmExample combinedGuaranteeConfirmExample = new CombinedGuaranteeConfirmExample(guaranteePaymentId);

        // 合并消费担保确认
        CombinedGuaranteeConfirmResp combinedGuaranteeConfirmResp = combinedGuaranteeConfirmExample.create();

        // 合并消费担保确认订单查询（系统订单号）
        combinedGuaranteeConfirmResp = combinedGuaranteeConfirmExample.retrieveById(combinedGuaranteeConfirmResp.getCombinedGuaranteeConfirmId());

        // 合并消费担保确认订单查询（平台订单号）
        combinedGuaranteeConfirmResp = combinedGuaranteeConfirmExample.retrieveByOutOrderNo(combinedGuaranteeConfirmResp.getOutOrderNo());

        return combinedGuaranteeConfirmResp.getCombinedGuaranteeConfirmId();
    }

    /**
     * 合并消费担保确认
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedGuaranteeConfirmResp create() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("guarantee_payment_id", this.guaranteePaymentId);
        params.put("out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("amount", 5);
        params.put("password", Upyzt.encryptField("pa55w0rd"));
        params.put("remark", "用于 SDK 示例测试");
        params.put("metadata", MockData.metadata());
        params.put("extra", MockData.extra());
        System.out.println("------- 合并消费担保确认 -------");
        CombinedGuaranteeConfirmResp resp = CombinedGuaranteeConfirm.create(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 合并消费担保确认订单查询（系统订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedGuaranteeConfirmResp retrieveById(String id) throws UpyztException {
        System.out.println("------- 合并消费担保确认订单查询（系统订单号） -------");
        CombinedGuaranteeConfirmResp resp = CombinedGuaranteeConfirm.retrieveById(id);
        System.out.println(resp);
        return resp;
    }

    /**
     * 合并消费担保确认订单查询（平台订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedGuaranteeConfirmResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 合并消费担保确认订单查询（平台订单号） -------");
        CombinedGuaranteeConfirmResp resp = CombinedGuaranteeConfirm.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }

}
